/*******************************************************************************
 * Package: com.hngtrust.iir.base.controller
 * Type:    OptLogController
 * Date:    2021-8-17 9:29
 *
 * Copyright (c) 2021 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.hngtrust.iir.base.controller;

import com.hngtrust.framework.bind.ResponseInfo;
import com.hngtrust.framework.page.PageParam;
import com.hngtrust.framework.page.PageResult;
import com.hngtrust.iir.base.controller.vo.ExportOptLogRespVO;
import com.hngtrust.iir.base.controller.vo.ListOnlineRespVO;
import com.hngtrust.iir.base.controller.vo.ListOptLogRespVO;
import com.hngtrust.iir.base.controller.vo.OptLogReqVO;
import com.hngtrust.iir.base.service.LogService;
import com.hngtrust.iir.dto.ReqDTO;
import com.hngtrust.iir.risk.service.ExcelDownloadService;
import com.hngtrust.iir.user.dto.ListOptLogReqVO;
import com.hngtrust.iir.util.DateUtils;
import com.tiger.web.annotation.DownloadMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 操作日志管理
 * @author Chentianyong
 * @date 2021-8-17 9:29
 */
@Slf4j
@Api(tags = "操作日志管理")
@RestController
@RequestMapping("/optlog")
public class OptLogController {
    
    @Autowired
    private LogService logService;
    
    @Autowired
    private ExcelDownloadService excelDownloadSevice;
    
    /**
     * 操作日志记录
     */
    @ApiOperation(value = "操作日志记录", notes = "操作日志记录")
    @PostMapping("/save-optlog")
    public ResponseInfo<Object> saveOptLog(
            @ApiParam(value = "操作日志记录") @Valid @RequestBody OptLogReqVO vo) {
        log.info("BaseController.saveOptLog start...");
        
        logService.saveOptLog(vo);
        
        return new ResponseInfo<>();
    }
    
    /**
     * 操作日志查询
     */
    @ApiOperation(value = "操作日志查询", notes = "操作日志查询")
    @PostMapping("/list-optlog")
    public ResponseInfo<PageResult<ListOptLogRespVO>> listOptLog(
            @ApiParam(value = "操作日志查询") @Valid @RequestBody ReqDTO<ListOptLogReqVO> vo) {
        log.info("BaseController.listOptLog start...");
    
        PageResult<ListOptLogRespVO> pageResult = logService.listOptLog(vo);
        
        return new ResponseInfo(pageResult);
    }
    
    /**
     * 获取在线用户
     */
    @ApiOperation(value = "获取在线用户", notes = "获取在线用户")
    @PostMapping("/list-online")
    public ResponseInfo<List<ListOnlineRespVO>> listOnline() {
        log.info("BaseController.listOnline start...");
    
        List<ListOnlineRespVO> resp = logService.listOnline();
        
        ResponseInfo<List<ListOnlineRespVO>> data = new ResponseInfo<>();
        data.setData(resp);
        return data;
    }
    
    /**
     * 登录日志查询
     */
    @ApiOperation(value = "登录日志查询", notes = "登录日志查询")
    @PostMapping("/list-loginlog")
    public ResponseInfo<PageResult<ListOptLogRespVO>> listLoginLog(
            @ApiParam(value = "登录日志查询") @Valid @RequestBody ReqDTO<ListOptLogReqVO> vo) {
        log.info("BaseController.listLoginLog start...");
        
        PageResult<ListOptLogRespVO> pageResult = logService.listLoginLog(vo);
        
        return new ResponseInfo(pageResult);
    }
    
    @ApiOperation("导出列表")
    @DownloadMapping("/export-list")
    public void exportList(@ApiParam("操作日志导出请求") @Valid ListOptLogReqVO reqDTO, HttpServletResponse response)
            throws Exception {
        PageParam pageParam = new PageParam();
        pageParam.setPageNum(1);
        pageParam.setPageSize(0);
        pageParam.setOrderBy(reqDTO.getOrderBy());
        ReqDTO<ListOptLogReqVO> req = new ReqDTO<>();
        req.setContent(reqDTO);
        req.setPageParam(pageParam);
        PageResult<ListOptLogRespVO> pageResult = logService.listOptLog(req);
        List<ExportOptLogRespVO> result = new ArrayList<>();
        for (ListOptLogRespVO obj:pageResult.getRows()){
            ExportOptLogRespVO respVO = new ExportOptLogRespVO();
            BeanUtils.copyProperties(obj,respVO);
            respVO.setCreateTime(DateUtils.format(obj.getCreateTime(),DateUtils.DATETIME));
            result.add(respVO);
        }
        excelDownloadSevice.excelDownload(result, response, "操作日志列表");
        
    }
    
    @ApiOperation("在线用户导出")
    @DownloadMapping("/export-online")
    public void exportOnline(@ApiParam("在线用户导出") @Valid ListOptLogReqVO reqDTO, HttpServletResponse response)
            throws Exception {
        PageParam pageParam = new PageParam();
        pageParam.setPageNum(1);
        pageParam.setPageSize(0);
        pageParam.setOrderBy(reqDTO.getOrderBy());
        ReqDTO<ListOptLogReqVO> req = new ReqDTO<>();
        req.setContent(reqDTO);
        req.setPageParam(pageParam);
        PageResult<ListOptLogRespVO> pageResult = logService.listLoginLog(req);
        List<ListOnlineRespVO> result = new ArrayList<>();
        for (ListOptLogRespVO obj:pageResult.getRows()){
            ListOnlineRespVO respVO = new ListOnlineRespVO();
            BeanUtils.copyProperties(obj,respVO);
            respVO.setName(obj.getUserName());
            respVO.setStatusName(obj.getLoginStatusName());
            respVO.setLoginTimeStr(DateUtils.format(obj.getCreateTime(),DateUtils.DATETIME));
            result.add(respVO);
        }
        excelDownloadSevice.excelDownload(result, response, "在线用户列表");
        
    }
}
